var express = require('express');
var router = express.Router();
const moviesApi = require("../api/movies")

/**,
 * @swagger
 * /movies:
 *    get:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 获取电影信息   # 接口备注
 *      description: 获取电影信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      parameters:
 *        - in: query
 *          name: curPage
 *          schema:
 *            type: integer
 *            default: 1
 *          required: true
 *          description: 当前页码
 *        - in: query
 *          name: eachPage
 *          schema:
 *            type: integer
 *            default: 10
 *          required: true
 *          description: 每页显示条数
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "获取成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/ArrayOfMovies'
 *
 * */

/* GET home page. */
//  /students/和/students是一样的
router.get('/', async function (req, res, next) {
    const { curPage, eachPage } = req.query;
    const data = await moviesApi.getMoviesData({ curPage, eachPage });
    res.send(data);
});

/**,
 * @swagger
 * /movies/addMovies:
 *    post:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 新增电影信息   # 接口备注
 *      description: 用于新增电影的基本信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *              cname: "了不起的老爸"
 *              ename: "On Your Mark"
 *              type: "喜剧 运动"
 *              area: "中国大陆 "
 *              time: 104
 *              upDate: "2021-06-18 08:00"
 *              score: 9.2
 *              count: "1.05亿"
 *              intro: "一个患有罕见疾病却怀有“马拉松梦”的少年肖尔东（张宥浩 饰），一个固执逼迫儿子预习“盲人生活”的出租车司机父亲肖大明（王砚辉 饰），互不理解的父子俩，却在一场马拉松比赛中“互换身份”，并肩站在了起跑线上……影片通过对“命运”持有不同态度的一对单亲家庭父子，讲述了一个用爱向阳而生的故事。"
 *              isClassic: false
 *              director: "周青元"
 *              actor: "王砚辉,张宥浩,龚蓓芯,李小胖"  
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "新增成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *              example:
 *                _id: "60d9dd0b3201fa4de4d1b342"
 *                cname: "了不起的老爸"
 *                ename: "On Your Mark"
 *                type: "喜剧 运动"
 *                area: "中国大陆 "
 *                time: 104
 *                upDate: "2021-06-18 08:00"
 *                score: 9.2
 *                count: "1.05亿"
 *                intro: "一个患有罕见疾病却怀有“马拉松梦”的少年肖尔东（张宥浩 饰），一个固执逼迫儿子预习“盲人生活”的出租车司机父亲肖大明（王砚辉 饰），互不理解的父子俩，却在一场马拉松比赛中“互换身份”，并肩站在了起跑线上……影片通过对“命运”持有不同态度的一对单亲家庭父子，讲述了一个用爱向阳而生的故事。"
 *                isClassic: false
 *                director: "周青元"
 *                actor: "王砚辉,张宥浩,龚蓓芯,李小胖"  
 *
 * */
router.post('/addMovies', async function (req, res, next) {
    // console.log(req.body);   
    const movies = req.body;
    const data = await moviesApi.addMovies(movies);
    res.send(data);
});

/**,
 * @swagger
 * /movies/updateMovies:
 *    post:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 通过_id获取电影信息   # 接口备注
 *      description: 用于通过_id获取电影信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *              _id: "60d9dd0b3201fa4de4d1b342"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "获取成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *
 * */
router.post('/updateMovies', async function (req, res, next) {
    const { _id } = req.body;
    await moviesApi.updateMovies({ _id });
    const data = await moviesApi.getOne({ _id })
    res.send(data);
});

/**,
 * @swagger
 * /movies/updateMoviesSave:
 *    patch:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 修改电影基本信息   # 接口备注
 *      description: 用于通过_id获取的电影信息来修改电影基本信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *              _id: "60d9dd0b3201fa4de4d1b342"
 *              cname: "了不起的老爸"
 *              ename: "On Your Mark"
 *              type: "喜剧 运动"
 *              area: "中国大陆 "
 *              time: 104
 *              upDate: "2021-06-18 08:00"
 *              score: 9.2
 *              count: "1.05亿"
 *              intro: "一个患有罕见疾病却怀有“马拉松梦”的少年肖尔东（张宥浩 饰），一个固执逼迫儿子预习“盲人生活”的出租车司机父亲肖大明（王砚辉 饰），互不理解的父子俩，却在一场马拉松比赛中“互换身份”，并肩站在了起跑线上……影片通过对“命运”持有不同态度的一对单亲家庭父子，讲述了一个用爱向阳而生的故事。"
 *              isClassic: false
 *              director: "周青元"
 *              actor: "王砚辉,张宥浩,龚蓓芯,李小胖"    
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "获取成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *
 * */
router.patch("/updateMoviesSave", async function (req, res, next) {
    const movies = req.body;
    const data = await moviesApi.update(movies);
    res.send(data);
});

/**,
 * @swagger
 * /movies/:_id/imgs/:url:
 *    patch:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 新增电影海报照片   # 接口备注
 *      description: 新增电影海报照片   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *              _id: "60d9dd0b3201fa4de4d1b342"
 *              cname: "了不起的老爸"
 *              ename: "On Your Mark"
 *              type: "喜剧 运动"
 *              area: "中国大陆 "
 *              time: 104
 *              upDate: "2021-06-18 08:00"
 *              score: 9.2
 *              count: "1.05亿"
 *              intro: "一个患有罕见疾病却怀有“马拉松梦”的少年肖尔东（张宥浩 饰），一个固执逼迫儿子预习“盲人生活”的出租车司机父亲肖大明（王砚辉 饰），互不理解的父子俩，却在一场马拉松比赛中“互换身份”，并肩站在了起跑线上……影片通过对“命运”持有不同态度的一对单亲家庭父子，讲述了一个用爱向阳而生的故事。"
 *              isClassic: false
 *              director: "周青元"
 *              actor: "王砚辉,张宥浩,龚蓓芯,李小胖"    
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "获取成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *
 * */
//新增海报图片
router.patch("/:_id/imgs/:url", async function (req, res, next) {
    // console.log(11);
    //  console.log(req.params);
    const { _id, url } = req.params;
    await moviesApi.addPostImgs({ _id, url });
    const data = await moviesApi.getOne({ _id })

    res.send(data);
});
//新增剧集图片
router.patch("/:_id/setImg/:url", async function (req, res, next) {
    // console.log(11);
    //  console.log(req.params);
    const { _id, url } = req.params;
    await moviesApi.addSetImgs({ _id, url });
    const data = await moviesApi.getOne({ _id })

    res.send(data);
});
//新增相关人物图片
router.patch("/:_id/headImg/:url", async function (req, res, next) {
    const { _id, url } = req.params;
    await moviesApi.addHeadImgs({ _id, url });
    const data = await moviesApi.getOne({ _id })
    res.send(data);
});
/**,
 * @swagger
 * /movies/:_id/imgs/:url/:PostImg:
 *    delete:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 删除某个电影的相关图片   # 接口备注
 *      description: 删除某个电影信息的相关图片   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *               _id: "60d9dd0b3201fa4de4d1b342"
 *               url: "/common/99227016f6c06.jpg"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "删除成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *
 * */
//删除图片
router.delete("/:_id/imgs/:url/:PostImg", async function (req, res, next) {
    // console.log(123);
    //  console.log(1,req.params);
    const { _id, url } = req.params;
    const types = req.params.PostImg
    switch (types) {
        case "PostImg":
            await moviesApi.deleteImgs({ _id, url }, types);
            break;
        case "HeadImg":
            await moviesApi.deleteImgs({ _id, url }, types);
            break;
        case "SetsImg":
            await moviesApi.deleteImgs({ _id, url }, types);
            break;
    }
    // await moviesApi.deletePostImgs({ _id, url });
    const data = await moviesApi.getOne(_id)
    res.send(data);
});


/**,
 * @swagger
 * /movies/delMovies:
 *    delete:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 删除某个电影信息   # 接口备注
 *      description: 通过_id删除某个电影信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *            example:
 *               _id: "60d9dd0b3201fa4de4d1b342"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "删除成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/ArrayOfMoves'
 *
 * */


router.delete('/delMovies', async function (req, res, next) {
    const { _id } = req.body;
    const data = await moviesApi.deleteMovies({ _id });
    res.send(data);
});
/**,
 * @swagger
 * /movies/searchMovies:
 *    post:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 通过查询条件查询电影信息   # 接口备注
 *      description: 用于通过查询条件查询电影信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/movies'
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "查询成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/movies'
 *
 * */
router.post('/searchMovies', async function (req, res, next) {
    const movies = req.body;
    const data = await moviesApi.serchMovies(movies);
    res.send(data);
});
/**,
 * @swagger
 * /movies/:_id:
 *    get:
 *      tags:
 *      - 电影列表管理模块          # 接口分类
 *      summary: 获取电影信息   # 接口备注
 *      description: 获取电影信息   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      parameters:
 *        - in: query
 *          name: curPage
 *          schema:
 *            type: integer
 *            default: 1
 *          required: true
 *          description: 当前页码
 *        - in: query
 *          name: eachPage
 *          schema:
 *            type: integer
 *            default: 10
 *          required: true
 *          description: 每页显示条数
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "获取成功"     # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/ArrayOfMovies'
 *
 * */
router.get('/:_id', async function (req, res, next) {
    //  console.log(req.params);
    const{ _id}=req.params;
    const data = await moviesApi.getOne({ _id});
    res.send( data);
});


module.exports = router;